<template>
  <div class="form-quality-rule">
    <form-group :value="groupValue" :field="qualityRuleField" @update="update">
      
      <!-- 质保起算时间 -->
      <template slot="qualityStartTime" slot-scope="{ field }">
        <form-item :label="field.displayName" :validation="validation.qualityStartTime">
           <product-type-quality-startTime :value="groupValue ? groupValue.qualityStartTime : {}" :field="field" @update="qualityStartTimeUpdate"></product-type-quality-startTime>
        </form-item>
      </template>
      <!-- 质保起算时间 -->

      <!-- start 质保时长 -->
      <template slot="qualityTime" slot-scope="{field}">
        <form-item :label="field.displayName" :validation="validation.qualityTime">
          <product-type-qualityTime-build :value="groupValue ? groupValue.qualityTime : {}" :field="field" @update="qualityTimeUpdate" />
        </form-item>
      </template>
      <!-- end 质保时长 -->
      
    </form-group>
  </div>
</template>

<script>
/* mixin */
import FormMixin from '@src/component/form/mixin/form'
/* model */
import { getGroupsFields } from '@src/component/form/components/FormQualityRuleField/FormQualityRuleModel.ts'
/* util */
import { isEmpty } from '@src/util/type'

export default {
  name: 'form-quality-rule',
  mixins: [FormMixin],
  props: {
    value: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    return {
      validation: this.buildValidation(),
      productValue: [],
    }
  },
  computed: {
    groupFields() {
      let displayName = this.field?.displayName || ''
      let isNull = this.field?.isNull
      let setting = this.field?.setting || {}
      return getGroupsFields(displayName, isNull, setting)
    },
    groupValue() {
      return this.value
    },
    qualityTimeValue() {
      return !isEmpty(this.groupValue) ? this.groupValue.qualityTime : {}
    },
    qualityStartTimeValue() {
      return !isEmpty(this.groupValue) ? this.groupValue.qualityStartTime : {}
    },
    qualityRuleField() {
      return {
        ...this.field,
        groupFields: this.groupFields
      }
    },
  },
  methods: {
    buildValidation(){ 
      const checkQualityTime = function(value, resolve, changeStatus) {
        resolve(isEmpty(value) ? this.$t('common.form.preview.qualityRuleField.tips1') : null)
      }
      
      return (
        Object.freeze({
          qualityTime(value, field, changeStatus){
            return new Promise((resolve, reject) => checkQualityTime(value, resolve, changeStatus))
          },
        })
      )
    },
    // 更新质保起算时间
    qualityStartTimeUpdate({ field, newValue }) {
      let value = {
        ...this.qualityStartTimeValue,
        [field.fieldName]: newValue
      }
      this.update({ newValue: value })
    },
    qualityTimeUpdate({ field, newValue }) {
      let value = {
        ...this.qualityTimeValue,
        [field.fieldName]: newValue
      }
      this.update({ newValue: value })
    },
    update({ field, newValue }) {
      newValue = Object.assign(this.groupValue, newValue)
      
      this.$emit('update', { newValue, field: this.field })
      this.$emit('input', newValue)
    },
  }
}
</script>

<style lang="scss">
.form-quality_rule {
  & > label {
    display: none !important;
  }
}
</style>
